package com.app.shop.service.shop.impl;

import com.app.shop.dao.wg.WgTearmMapper;
import com.app.shop.entity.WgFocus;
import com.app.shop.entity.WgTearm;
import com.app.shop.entity.WgTearmExample;
import com.app.shop.entity.query.WgFocusQuery;
import com.app.shop.entity.query.WgTearmQuery;
import com.app.shop.entity.show.WxModel;
import com.app.shop.service.shop.WgTearmService;
import com.app.shop.util.Constant;
import com.app.shop.util.SysModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Garfield Miao
 * @Title web-shop
 * @Description 团队服务
 * @date 2019年11月26日
 * @upauthor Garfield Miao
 * @update 2019年11月26日
 */
@Service("wgTearmService")
public class WgTearmServiceImp implements WgTearmService {
    private static final Logger log = LoggerFactory.getLogger(WgTearmServiceImp.class);

    @Autowired
    private WgTearmMapper wgTearmMapper;


    @Override
    public List<WxModel> getTearmList() throws Exception {
        WgTearmExample WgTearmExample = new WgTearmExample();
        WgTearmExample.Criteria criteria = WgTearmExample.createCriteria();
        criteria.andDeleteFlagEqualTo(1);
        List<WgTearm> wgTearms = wgTearmMapper.selectByExample(WgTearmExample);
        List<WxModel> wxModels = new ArrayList<WxModel>();
        for (WgTearm wgtearm : wgTearms) {
            wxModels.add(new WxModel(wgtearm.getName(), Constant.getDic(10022, wgtearm.getLevel()), Integer.parseInt(wgtearm.getScore()), wgtearm.getDescription(), wgtearm.getUrl()));
        }

        return wxModels;
    }

    @Override
    public Page<WgTearm> getWgTearmList(WgTearmQuery wgTearmQuery) {
        //查询条件
        WgTearmExample wgTearmExample = new WgTearmExample();
        WgTearmExample.Criteria criteria = wgTearmExample.createCriteria();
        //图片名称
        if (wgTearmQuery.getName() != null && wgTearmQuery.getName().trim().length() > 0) {
            criteria.andNameLike('%' + wgTearmQuery.getName() + '%');
        }
        //商品上架状态
        if (wgTearmQuery.getStatus() != null ) {
            criteria.andStatusEqualTo(wgTearmQuery.getStatus());
        }
        //商品有效失效状态
        if (wgTearmQuery.getDeleteFlag() != null) {
            criteria.andDeleteFlagEqualTo(wgTearmQuery.getDeleteFlag());
        }
        //模拟分页查询
        PageHelper.startPage(wgTearmQuery.getCurrentPage(), wgTearmQuery.getPageSize());
        return wgTearmMapper.selectByExample(wgTearmExample);
    }

    @Override
    public WgTearm getTearmById(WgTearm wgTearm) {
        return wgTearmMapper.selectByPrimaryKey(wgTearm.getId());
    }

    @Override
    public SysModel editTearmDelete(WgTearm wgTearm) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgTearmExample wgTearmExample = new WgTearmExample();
            WgTearmExample.Criteria criteria = wgTearmExample.createCriteria();
            criteria.andIdEqualTo(wgTearm.getId()).andDeleteFlagEqualTo(1);
            WgTearm sysMenuUp = wgTearmMapper.selectByPrimaryKeyMore(wgTearmExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgTearm.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgTearm.getModifiedDate());
                sysMenuUp.setStatus(2);
                sysMenuUp.setDeleteFlag(2);
                wgTearmMapper.updateByExample(sysMenuUp, wgTearmExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("教练修改失败，删除教练必须为有效的教练！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("教练修改失败，修改过程发生异常请重试！");
            log.error("教练修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    public SysModel addTearm(WgTearm wgTearm) {
        SysModel sysModel = new SysModel();
        try {
            wgTearm.setStatus(2);
            wgTearm.setDeleteFlag(1);
            wgTearmMapper.insert(wgTearm);
            sysModel.setSuccess(true);
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("教练添加失败，修改过程发生异常请重试！");
            log.error("教练添加过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    public SysModel editTearm(WgTearm wgTearm) {
        SysModel sysModel = new SysModel();
        try {
            WgTearmExample wgTearmExample = new WgTearmExample();
            WgTearmExample.Criteria criteria = wgTearmExample.createCriteria();
            criteria.andDeleteFlagEqualTo(1).andIdEqualTo(wgTearm.getId());
            WgTearm wgTearmUp = wgTearmMapper.selectByPrimaryKeyMore(wgTearmExample);
            if (wgTearmUp != null) {
                wgTearmUp.setModifiedUserId(wgTearm.getModifiedUserId());
                wgTearmUp.setModifiedDate(wgTearm.getModifiedDate());
                wgTearmUp.setName(wgTearm.getName());
                wgTearmUp.setDescription(wgTearm.getDescription());
                wgTearmUp.setLevel(wgTearm.getLevel());
                wgTearmUp.setScore(wgTearm.getScore());
                wgTearmUp.setUrl(wgTearm.getUrl());
                wgTearmMapper.updateByExample(wgTearmUp, wgTearmExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("教练修改失败，当前教练已经被删除，不允许再次修改！");
            }

        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("教练修改失败，修改过程发生异常请重试！");
            log.error("教练修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editTearmUp(WgTearm wgTearm) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgTearmExample wgTearmExample = new WgTearmExample();
            WgTearmExample.Criteria criteria = wgTearmExample.createCriteria();
            criteria.andIdEqualTo(wgTearm.getId()).andDeleteFlagEqualTo(1).andStatusEqualTo(2);
            WgTearm sysMenuUp = wgTearmMapper.selectByPrimaryKeyMore(wgTearmExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgTearm.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgTearm.getModifiedDate());
                sysMenuUp.setStatus(1);
                wgTearmMapper.updateByExample(sysMenuUp, wgTearmExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("教练修改失败，上架教练必须为上架的有效的教练！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("教练修改失败，修改过程发生异常请重试！");
            log.error("教练修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel editTearmDown(WgTearm wgTearm) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            WgTearmExample wgTearmExample = new WgTearmExample();
            WgTearmExample.Criteria criteria = wgTearmExample.createCriteria();
            criteria.andIdEqualTo(wgTearm.getId()).andDeleteFlagEqualTo(1).andStatusEqualTo(1);
            WgTearm sysMenuUp = wgTearmMapper.selectByPrimaryKeyMore(wgTearmExample);
            if (sysMenuUp != null) {
                sysMenuUp.setModifiedUserId(wgTearm.getModifiedUserId());
                sysMenuUp.setModifiedDate(wgTearm.getModifiedDate());
                sysMenuUp.setStatus(2);
                wgTearmMapper.updateByExample(sysMenuUp, wgTearmExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("教练修改失败，下架教练必须为已上架有效的教练！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("教练修改失败，修改过程发生异常请重试！");
            log.error("教练修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }
}
